#include "file_reader.h"
#include <QLineF>
#include <QPointF>

FileReader::FileReader(const QString fileName)
{
    this->fileName = fileName;
}

void FileReader::readMap(){

    //criando a lista de pontos a partir do arquivo

    //bool aberto = false;
    QStringList listName = this->fileName.split("/");

    QString tempName = listName.at(listName.size() -1);
    this->mapName = tempName.remove(".txt",Qt::CaseInsensitive);

    QFile file(this->fileName);

        if (!file.open(QIODevice::ReadOnly | QIODevice::Text)){
            qDebug () << "Falhou em abrir arquivo";
            QString path = QDir::currentPath();
            qDebug() << path;
            return;
        }
            QStringList list;
            QTextStream in(&file);
            while (!in.atEnd()) {
                list << in.readLine();
            }
            double datax[list.size()] ;
            double datay[list.size()] ;
            double datacolor[list.size()] ;

            for(int i = 0; i< list.size(); i++){

                QString pointList = list.at(i);
                QStringList sublist = pointList.split("\t");

                datay[i] = sublist[0].toDouble();
                datax[i] = sublist[1].toDouble();
                datacolor[i] = sublist[2].toDouble();
                this->points.append(Point(datax[i], datay[i], datacolor[i]));
            }

            this->maxX = 0;
            this->maxY = 0;
            this->minorX = datax[0];
            this->minorY = datay[0];

            for(int i = 0; i< list.size(); i++){

                if(this->maxX <= datax[i])
                    this->maxX = qRound(datax[i]);

                if(this->maxY <= datay[i])
                    this->maxY = qRound(datay[i]);

                if(this->minorX >= datax[i])
                    this->minorX = qRound(datax[i]);

                if(this->minorY >= datay[i])
                   this->minorY = qRound(datay[i]);
            }

}


Points FileReader::readMapKriging(){

    Points allPoints;
   // qDebug()<< "READ MAP KRIGING";

    QFile file(this->fileName);

        if (!file.open(QIODevice::ReadOnly | QIODevice::Text)){
            qDebug () << "Falhou em abrir arquivo";
            QString path = QDir::currentPath();
            qDebug() << path;
        }

            QStringList list;
            QTextStream in(&file);
            while (!in.atEnd()) {
                list << in.readLine();
            }

             qDebug()<< "list.size() " + list.size();
            double datax[list.size()] ;
            double datay[list.size()] ;
            double datacolor[list.size()] ;

            for(int i = 0; i< list.size(); i++){

               // qDebug()<< "Lifjhgbnnha " << i;
                QString pointList = list.at(i);
                QStringList sublist = pointList.split("\t");

                datay[i] = sublist[0].toDouble();
                datax[i] = sublist[1].toDouble();
                datacolor[i] = sublist[2].toDouble();


//                qDebug()<< "datax " << datax[i];

//                qDebug()<< " datay " << datay[i];

//                qDebug()<< "value " << datacolor[i];



                allPoints.addPoint(Point(datax[i], datay[i], datacolor[i]));

            }

            this->maxX = 0;
            this->maxY = 0;
            this->minorX = datax[0];
            this->minorY = datay[0];

            for(int i = 0; i< list.size(); i++){

                if(this->maxX <= datax[i])
                    this->maxX = qRound(datax[i]);

                if(this->maxY <= datay[i])
                    this->maxY = qRound(datay[i]);

                if(this->minorX >= datax[i])
                    this->minorX = qRound(datax[i]);

                if(this->minorY >= datay[i])
                   this->minorY = qRound(datay[i]);
            }


            return allPoints;
}


